<template>
  <header class="header">
    <!-- 粒子背景画布 -->
    <canvas ref="particleCanvas" class="particle-canvas"></canvas>

    <div class="header-left">
      <img src="@/assets/img/school.png" alt="" class="logo-img" />
      <div class="header-text">
        <span class="college">JPSMAI全景智能安防与多模态大模型实验室</span>
        <br />
        <span class="english-name"
          >Joint Research Lab on Panoramic Intelligent Surveillance and Multimodal Large Models</span
        >
      </div>
    </div>

    <!-- 汉堡菜单按钮（小屏显示） -->
    <button class="menu-btn" @click="toggleMenu" v-if="isMobile">
      <span class="menu-icon"></span>
    </button>

    <!-- 导航区域 -->
    <nav class="nav" :class="{ 'mobile-nav': isMobile, 'nav-open': isMenuOpen }">
      <ul>
        <li>
          <router-link to="/" exact style="color: white" @click="isMobile && (isMenuOpen = false)">首页</router-link>
        </li>
        <li
          class="dropdown"
          @mouseenter="toggleLabInfoDropdown"
          @mouseleave="toggleLabInfoDropdown"
          @click="isMobile && toggleLabInfoDropdown"
        >
          <a href="#" class="nav-link" :class="{ active: isLabInfoDropdownOpen }" style="color: white">实验室概况</a>
          <div class="dropdown-menu" :class="{ 'dropdown-open': isLabInfoDropdownOpen }">
            <div
              class="dropdown-item"
              @click="
                goTo('/labInstruction');
                isMobile && (isMenuOpen = false);
              "
            >
              实验室简介
            </div>
            <div
              class="dropdown-item"
              @click="
                goTo('/labStructure');
                isMobile && (isMenuOpen = false);
              "
            >
              组织架构
            </div>
          </div>
        </li>
        <li>
          <router-link to="/researchTeam" style="color: white" @click="isMobile && (isMenuOpen = false)"
            >科研队伍</router-link
          >
        </li>
        <li
          class="dropdown"
          @mouseenter="toggleLabResearchDropdown"
          @mouseleave="toggleLabResearchDropdown"
          @click="isMobile && toggleLabResearchDropdown"
        >
          <a href="#" class="nav-link" :class="{ active: isLabResearchDropdownOpen }" style="color: white">科学研究</a>
          <div class="dropdown-menu" :class="{ 'dropdown-open': isLabResearchDropdownOpen }">
            <div
              class="dropdown-item"
              @click="
                goTo('/researchDirection');
                isMobile && (isMenuOpen = false);
              "
            >
              研究方向
            </div>
            <div
              class="dropdown-item"
              @click="
                goTo('/researchAchievement');
                isMobile && (isMenuOpen = false);
              "
            >
              科研成果
            </div>
          </div>
        </li>
        <li>
          <router-link to="/newestProduction" style="color: white" @click="isMobile && (isMenuOpen = false)"
            >成果转化</router-link
          >
        </li>
        <li>
          <router-link to="/mall" style="color: white">算法试用</router-link>
        </li>
      </ul>
    </nav>
  </header>
</template>

<script>
export default {
  name: 'AppHeader',
  components: {
  },
  data() {
    return {
      showDemandModal: false,
      isLabInfoDropdownOpen: false,
      isLabResearchDropdownOpen: false,
      particles: [],
      animationId: null,
      isMenuOpen: false,
      isMobile: false,
    }
  },
  methods: {
    handleSubmitSuccess(formData) {
      console.log('表单提交成功:', formData);
    },
    toggleLabInfoDropdown() {
      this.isLabInfoDropdownOpen = !this.isLabInfoDropdownOpen;
    },
    toggleLabResearchDropdown() {
      this.isLabResearchDropdownOpen = !this.isLabResearchDropdownOpen;
    },
    goTo(url) {
      this.$router.push(url);
    },
    toggleMenu() {
      this.isMenuOpen = !this.isMenuOpen;
      this.isLabInfoDropdownOpen = false;
      this.isLabResearchDropdownOpen = false;
    },
    checkScreenSize() {
      this.isMobile = window.innerWidth < 768;
      if (!this.isMobile) {
        this.isMenuOpen = false;
      }
    },
    initParticles() {
      const canvas = this.$refs.particleCanvas;
      if (!canvas) return;

      const ctx = canvas.getContext('2d');

      const resizeCanvas = () => {
        canvas.width = this.$el.offsetWidth;
        canvas.height = this.$el.offsetHeight;
      };

      resizeCanvas();
      window.addEventListener('resize', resizeCanvas);

      const particleCount = 50;
      this.particles = [];

      for (let i = 0; i < particleCount; i++) {
        this.particles.push({
          x: Math.random() * canvas.width,
          y: Math.random() * canvas.height,
          radius: Math.random() * 3 + 1,
          color: `rgba(255, 255, 255, ${Math.random() * 0.5 + 0.2})`,
          speedX: Math.random() * 0.5 - 0.25,
          speedY: Math.random() * 0.5 - 0.25
        });
      }

      const animate = () => {
        ctx.clearRect(0, 0, canvas.width, canvas.height);

        this.particles.forEach(particle => {
          particle.x += particle.speedX;
          particle.y += particle.speedY;

          if (particle.x < 0) particle.x = canvas.width;
          if (particle.x > canvas.width) particle.x = 0;
          if (particle.y < 0) particle.y = canvas.height;
          if (particle.y > canvas.height) particle.y = 0;

          ctx.beginPath();
          ctx.arc(particle.x, particle.y, particle.radius, 0, Math.PI * 2);
          ctx.fillStyle = particle.color;
          ctx.fill();

          this.particles.forEach(otherParticle => {
            const dx = particle.x - otherParticle.x;
            const dy = particle.y - otherParticle.y;
            const distance = Math.sqrt(dx * dx + dy * dy);

            if (distance < 100) {
              ctx.beginPath();
              ctx.strokeStyle = `rgba(255, 255, 255, ${0.2 - distance / 500})`;
              ctx.lineWidth = 0.5;
              ctx.moveTo(particle.x, particle.y);
              ctx.lineTo(otherParticle.x, otherParticle.y);
              ctx.stroke();
            }
          });
        });

        this.animationId = requestAnimationFrame(animate);
      };

      animate();
    }
  },
  mounted() {
    window.addEventListener('resize', this.checkScreenSize);
    this.checkScreenSize();
    this.$nextTick(() => {
      this.initParticles();
    });
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.checkScreenSize);
    if (this.animationId) {
      cancelAnimationFrame(this.animationId);
    }
  }
};
</script>

<style scoped>
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1.5vh 2vw;
  min-height: 12vh;
  position: relative;
  z-index: 100;
  background-image: url("../assets/img/test.png");
  background-size: cover;
  background-position: center;
  margin: 0;
  width: 100%;
  box-sizing: border-box;
}

.particle-canvas {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
  pointer-events: none;
}

.header-left {
  display: flex;
  align-items: center;
  position: relative;
  z-index: 2;
  flex: 1;
  min-width: 0;
}

.header-text {
  color: white;
  font-weight: bold;
  min-width: 0;
  flex-shrink: 1;
}

/* 优化字体大小，使用更精确的响应式单位 */
.college {
  font-size: clamp(16px, 2.5vw, 28px);
  color: #fff;
  text-shadow: 0 0 5px #7c04e5, 0 0 10px #8026d0, 0 0 20px #4012d8, 0 0 30px #2846ca, 0 0 40px #7928ca;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.2;
}

.english-name {
  font-size: clamp(10px, 1vw, 14px);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.3;
}

.logo-img {
  width: clamp(50px, 8vw, 250px);
  height: auto;
  margin-right: 1vw;
  object-fit: contain;
  position: relative;
  z-index: 2;
  flex-shrink: 0;
}

.nav {
  position: relative;
  z-index: 2;
  flex-shrink: 0;
}

.nav ul {
  list-style: none;
  display: flex;
  margin: 0;
  padding: 0;
  gap: 0.5vw;
  white-space: nowrap;
}

.nav li {
  margin: 0 0.5vw;
  font-size: clamp(14px, 1.1vw, 18px);
  font-weight: 400;
  position: relative;
}

.nav li a {
  padding: 5px 0;
  text-decoration: none;
  color: #333;
  cursor: pointer;
}

.nav li a.router-link-active {
  color: #00af84;
  border-bottom: 3px solid #00af84;
}

.nav li a.active {
  color: #00af84;
  border-bottom: 3px solid #00af84;
}

.login-btn {
  padding-top: 2px;
  background-color: #00af84;
  color: #fff;
  border: none;
  cursor: pointer;
  font-size: clamp(12px, 0.9vw, 15px);
  height: 36px;
  line-height: 36px;
  width: clamp(80px, 7vw, 100px);
  border-radius: 8px;
  overflow: hidden;
  margin-right: 0.5vw;
  position: relative;
  z-index: 2;
  flex-shrink: 0;
}

.dropdown {
  cursor: pointer;
  position: relative;
}

.dropdown-menu {
  position: absolute;
  top: 160%;
  left: 50%;
  transform: translateX(-50%) scale(0.95);
  transform-origin: top center;
  background: white;
  border-radius: 5px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.12);
  width: 120px;
  padding: 12px 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
  z-index: 1000;
  opacity: 0;
  visibility: hidden;
  transition: all 0.5s cubic-bezier(0.4, 0, 0.2, 1);
  border: none;
  overflow: hidden;
}

.dropdown-menu.dropdown-open {
  opacity: 1;
  visibility: visible;
  transform: translateX(-50%) scale(1);
}

.dropdown-item {
  padding: 12px 20px;
  text-align: left;
  color: #333;
  cursor: pointer;
  transition: all 0.2s ease;
  position: relative;
  overflow: hidden;
  background: transparent;
  border: none;
  font-size: 16px;
}

.dropdown-item:hover {
  background-color: #f5f7ff;
  color: #6915d0;
  padding-left: 24px;
}

.dropdown-item:hover::before {
  content: "";
  position: absolute;
  left: 0;
  top: 0;
  height: 100%;
  width: 4px;
  background-color: #6915d0;
}

.dropdown-item:not(:last-child) {
  border-bottom: 1px solid #f0f0f0;
}

.dropdown-menu::before {
  content: "";
  position: absolute;
  top: -8px;
  left: 50%;
  transform: translateX(-50%);
  border-left: 8px solid transparent;
  border-right: 8px solid transparent;
  border-bottom: 8px solid white;
  box-shadow: 0 -2px 5px rgba(0, 0, 0, 0.05);
}

.menu-btn {
  display: none;
  background: transparent;
  border: none;
  cursor: pointer;
  z-index: 1001;
  padding: 10px;
}

.menu-icon {
  display: block;
  width: 24px;
  height: 3px;
  background: white;
  position: relative;
}

.menu-icon::before,
.menu-icon::after {
  content: "";
  position: absolute;
  width: 24px;
  height: 3px;
  background: white;
  left: 0;
  transition: all 0.3s ease;
}

.menu-icon::before {
  top: -8px;
}

.menu-icon::after {
  bottom: -8px;
}

/* 中等屏幕优化 */
@media (max-width: 1200px) {
  .header {
    padding: 1.5vh 3vw;
  }

  .nav ul {
    gap: 0.3vw;
  }

  .nav li {
    margin: 0 0.3vw;
  }
}

/* 平板设备优化 */
@media (max-width: 1024px) {
  .header {
    padding: 1.5vh 4vw;
  }

  .college {
    font-size: clamp(16px, 2.8vw, 24px);
  }

  .english-name {
    font-size: clamp(10px, 1.2vw, 12px);
  }

  .nav li {
    font-size: clamp(14px, 1.3vw, 16px);
  }
}

/* 移动设备优化 */
@media (max-width: 767px) {
  .header {
    padding: 1.5vh 5vw;
    min-height: 10vh;
  }

  .menu-btn {
    display: block;
  }

  .header-left {
    gap: 4px;
  }

  .nav {
    position: fixed;
    top: 0;
    right: 0;
    height: 100vh;
    width: 70%;
    max-width: 300px;
    background: rgba(30, 30, 50, 0.95);
    transform: translateX(100%);
    transition: transform 0.3s ease;
    z-index: 1000;
  }

  .nav.nav-open {
    transform: translateX(0);
  }

  .mobile-nav ul {
    flex-direction: column;
    padding: 20px;
    margin-top: 80px;
  }

  .mobile-nav li {
    margin: 10px 0;
    padding: 10px 0;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    font-size: 16px;
  }

  .mobile-nav .dropdown-menu {
    position: static;
    transform: none;
    width: 100%;
    background: rgba(255, 255, 255, 0.1);
    box-shadow: none;
    padding-left: 20px;
  }

  .mobile-nav .dropdown-item {
    color: white;
    font-size: 14px;
  }

  .mobile-nav .dropdown-item:hover {
    background-color: rgba(255, 255, 255, 0.2);
  }

  .login-btn-mobile {
    margin-top: 20px;
    text-align: center;
  }

  .login-btn-mobile .login-btn {
    margin: 0 auto;
    width: 80%;
    font-size: 16px;
  }

  .college {
    font-size: clamp(14px, 4vw, 20px);
  }

  .english-name {
    font-size: clamp(8px, 2.5vw, 10px);
  }

  .logo-img {
    width: clamp(40px, 10vw, 60px);
  }
}

@media (min-width: 768px) {
  .login-btn-mobile {
    display: none;
  }
}
</style>